package com.im323.common.util;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import com.im323.ip.IpConverter;
import com.im323.ip.impl.Ip123ChaConvert;
import com.im323.ip.impl.Ip138Convert;
import com.im323.ip.impl.Ip360Convert;
import com.im323.ip.impl.IpCZConvert;
import com.im323.ip.impl.IpChinaZConvert;
import com.im323.ip.impl.IpMyIpConvert;
import com.im323.ip.impl.IpTBConvert;
import com.im323.ip.impl.IpXinHuaConvert;

public class IpUtil {

	/** IP138 */
	public static final String URL_IP138 = "http://2017.ip138.com/ic.asp";
	public static final String URL_IPCN = "http://ip.cn/";
	public static final String URL_TB = "http://ip.taobao.com/service/getIpInfo2.php?ip=myip";
	public static final String URL_IP360 = "http://ip.360.cn/IPShare/info";
	public static final String URL_MYIP = "http://myip.com.tw";
	public static final String URL_XIANHUA = "http://ip.xianhua.com.cn/";
	public static final String URL_123CHA = "http://www.123cha.com/ip";
	public static final String URL_CHINAZ = "http://ip.chinaz.com/getip.aspx";
	public static final String URL_CZ = "http://www.cz88.net/ip/";

	private static List<IpConverter> ipCounvertList = new ArrayList<>();
	private static List<Class<?>> ipCounvertClassList = new ArrayList<>();
	private static boolean initFlag = false;
	
	static {
		ipCounvertClassList.add(Ip138Convert.class);
		ipCounvertClassList.add(IpTBConvert.class);
		ipCounvertClassList.add(Ip360Convert.class);
		ipCounvertClassList.add(IpMyIpConvert.class);
		ipCounvertClassList.add(IpXinHuaConvert.class);
		ipCounvertClassList.add(Ip123ChaConvert.class);
		ipCounvertClassList.add(IpChinaZConvert.class);
		ipCounvertClassList.add(IpCZConvert.class);

		for (int i = 0; i < ipCounvertClassList.size(); i++) {
			Class<?> class1 = ipCounvertClassList.get(i);
			try {
				IpConverter newInstance = (IpConverter) class1.newInstance();
				ipCounvertList.add(newInstance);
			} catch (InstantiationException | IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	public static void init() {
		if(!initFlag) {
			for (Iterator<IpConverter> iterator = ipCounvertList.iterator(); iterator.hasNext();) {
				IpConverter ipConverter = (IpConverter) iterator.next();
				IpModel ipModel = ipConverter.parse();
				if (ipModel == null) {
					ipConverter.delay = 999999L;
				} else {
					ipConverter.delay = ipModel.getDelay();
				}
			}
		}
		ipCounvertList.sort(new Comparator<IpConverter>() {

			@Override
			public int compare(IpConverter o1, IpConverter o2) {
				if (o1 == null && o2 == null) {
					return 0;
				}
				if (o1 == null) {
					return 1;
				}
				if (o2 == null) {
					return -1;
				}
				return Long.compare(o1.delay, o2.delay);
			}
		});
	}

	public static synchronized IpModel getIpBySpeed() {
		init();
		for (int i = 0; i < ipCounvertList.size(); i++) {
			IpModel ipModel = ipCounvertList.get(0).parse();
			if (ipModel == null) {
				ipCounvertList.get(i).delay = 999999L;
				continue;
			}
			ipCounvertList.get(i).delay = ipModel.getDelay();
			return ipModel;
		}
		return null;
	}

	public static void main(String[] args) {
		getIpBySpeed();
	}

}
